<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="0">
<title>登录</title>
<!-- Icons -->
<link rel="stylesheet" type="text/css" href="/css/font-awesome/css/font-awesome.min.css"></link>
<link rel="stylesheet" type="text/css" href="/css/simple-line-icons/css/simple-line-icons.css"></link>
<link href="/css/login.css" type="text/css" rel="stylesheet">
 <!--layui and necessary plugins-->
<link rel="stylesheet" type="text/css" href="/lib/layui/css/layui.css"></link>
<!-- Main styles for this application -->
<link rel="stylesheet" type="text/css" href="/css/style.css"></link>
</head>
<body class="app flex-row align-items-center">
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-9">
            <div class="card-group">
                <div class="card p-6">
                    <div class="card-body">
                        <h1>登录</h1>
                        <p class="text-muted">登录到您的帐户</p>
                        <form id="login-form" method="post" onsubmit="return false;">
                            <div class="input-group mb-3">
                                <span class="input-group-addon"><i class="icon-user"></i></span>
                                <input type="text" name="username" id="username" class="form-control"  placeholder="用户名"/>
                            </div>
                            <div class="input-group mb-4">
                                <span class="input-group-addon"><i class="icon-lock"></i></span>
                                <input type="password" name="password" id="password" class="form-control" placeholder="密码"/>
                            </div>
                            <div class="row">
                                <div class="col-6">
                                    <button class="layui-btn layui-btn-sm"  type="submit"
										onclick="login(this)">登录</button>
                                </div>
                                <div class="col-6 text-right">
                                    <span id="info" style="color: red"></span>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
                <div class="card p-6">
                    <script src='//gitee.com/owenwangwen/open-capacity-platform/widget_preview'></script>
                </div>
            </div>
        </div>
    </div>
</div>


</body>
<script src="/js/libs/jquery-2.1.1.min.js"></script>
<script type="text/javascript">
	if (top != self) {
		parent.location.href = '/login.html';
	}
	
	var token = localStorage.getItem("token");
	if (token != null && token.trim().length != 0) {
		$.ajax({
			type : 'get',
			url : '/users/current?token=' + token,
			success : function(data) {
				location.href = '/index.html';
			},
			error : function(xhr, textStatus, errorThrown) {
				var msg = xhr.responseText;
				var response = JSON.parse(msg);
				var code = response.code;
				var message = response.message;
				if (code == 401) {
					localStorage.removeItem("token");
				}
			}
		});
	}

	function login(obj) {
		$(obj).attr("disabled", true);

		var username = $.trim($('#username').val());
		var password = $.trim($('#password').val());
		if (username == "" || password == "") {
			$("#info").html('用户名或者密码不能为空');
			$(obj).attr("disabled", false);
		} else {
			$.ajax({
				type : 'post',
				url : '/login',
				data : $("#login-form").serialize(),
				success : function(data) {
					localStorage.setItem("token", data.token);
					location.href = '/index.html';
				},
				error : function(xhr, textStatus, errorThrown) {
					var msg = xhr.responseText;
					var response = JSON.parse(msg);
					$("#info").html(response.message);
					$(obj).attr("disabled", false);
				}
			});

		}
	}
</script>
</html>